
package com.wyzz.hr.action;

import java.io.File;
import java.util.UUID;
import org.apache.axis.utils.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IEmpregistryBO;
import com.wyzz.hr.pojo.Empregistry;

public class EmpRegistryDWR extends BaseAction {

	public EmpRegistryDWR() {
	}

	public String empRegistryAdd() {
		empRegistryBO = (IEmpregistryBO) getBean("empRegistryBO");
		if (fileFileName != null && !"".equals(fileFileName)) {
			String UploadResult;
			String pathConfig = "sys.profile.file.path";
			String typeConfig = "sys.profile.file.type";
			String lengthConfig = "sys.profile.file.length";
			String postfix = fileFileName.substring(fileFileName
					.lastIndexOf("."));
			fileFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
					lengthConfig, new String[] { fileFileName });
			if ("property".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
				return "error";
			}
			try {
				empRegistry.setEregAttachment(fileFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		empRegistryBO.insert(empRegistry);
		return "success";
	}

	public String eregDelete(String eregId) {
		String auth = DWRUtil.checkAuth("empregistrydwr", "");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		empRegistryBO = (IEmpregistryBO) getBean("empRegistryBO");
		empRegistry = empRegistryBO.loadById(eregId, null);
		if ("SUB".equals(auth)
				&& !checkAuth(empRegistry.getEregEmpNo().getId())) {
			return "noauth";
		} else {
			empRegistryBO.deleteAttach(eregId, empRegistry.getEregAttachment());
			empRegistryBO.delete(empRegistry);
			return "success";
		}
	}

	public Empregistry loadEreg(String eregId) {
		String auth = DWRUtil.checkAuth("empregistrydwr", "");
		if ("error".equalsIgnoreCase(auth))
			return null;
		if (StringUtils.isEmpty(eregId)) {
			return null;
		} else {
			empRegistryBO = (IEmpregistryBO) getBean("empRegistryBO");
			Empregistry ereg = empRegistryBO.loadById(eregId, null);
			return ereg;
		}
	}

	public String eregUpdate() {
		Empregistry ereg;
		empRegistryBO = (IEmpregistryBO) getBean("empRegistryBO");
		ereg = empRegistryBO.loadById(empRegistry.getEregId(), null);
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(ereg.getEregEmpNo().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		if (fileFileName != null && !"".equals(fileFileName)) {
			String UploadResult;
			String pathConfig = "sys.profile.file.path";
			String typeConfig = "sys.profile.file.type";
			String lengthConfig = "sys.profile.file.length";
			String postfix = fileFileName.substring(fileFileName
					.lastIndexOf("."));
			fileFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
					lengthConfig, new String[] { fileFileName,
							ereg.getEregAttachment() });
			if ("property".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
				return "error";
			}
			try {
				ereg.setEregAttachment(fileFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		ereg.setEregSpeciality(empRegistry.getEregSpeciality());
		ereg.setEregQualifyNo(empRegistry.getEregQualifyNo());
		ereg.setEregQualifyDate(empRegistry.getEregQualifyDate());
		ereg.setEregRegisterNo(empRegistry.getEregRegisterNo());
		ereg.setEregRegisterDate(empRegistry.getEregRegisterDate());
		ereg.setEregChartNo(empRegistry.getEregChartNo());
		ereg.setEregComments(empRegistry.getEregComments());
		empRegistryBO.update(ereg);
		addSuccessInfo("\u66F4\u65B0\u6CE8\u518C\u6267\u4E1A\u8D44\u683C\u6210\u529F\uFF01");
		return "success";
	}

	public String attachDelete() throws Exception {
		if (eregId == null || eregId.equals("") || fileFileName == null
				|| "".equals(fileFileName)) {
			addActionError("\u53C2\u6570\u4F20\u9012\u9519\u8BEF\uFF01");
			return "error";
		}
		empRegistryBO = (IEmpregistryBO) getBean("empRegistryBO");
		Empregistry ereg = empRegistryBO.loadById(eregId, null);
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(ereg.getEregEmpNo().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u5220\u9664\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		if (!empRegistryBO.deleteAttach(eregId, fileFileName)) {
			addActionError("\u9644\u4EF6\u5220\u9664\u5931\u8D25\uFF01");
			return "error";
		} else {
			addActionMessage("\u9644\u4EF6\u5220\u9664\u6210\u529F\u3002");
			return "success";
		}
	}

	public Empregistry getEmpRegistry() {
		return empRegistry;
	}

	public void setEmpRegistry(Empregistry empRegistry) {
		this.empRegistry = empRegistry;
	}

	public String getEregId() {
		return eregId;
	}

	public void setEregId(String eregId) {
		this.eregId = eregId;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	private static final long serialVersionUID = 1L;
	private Empregistry empRegistry;
	private String eregId;
	private File file;
	private String fileFileName;
	private IEmpregistryBO empRegistryBO;
	private String empNo;
	private String empName;
}
